<#if invalid?? && invalid>
<div class="alert alert-danger" role="alert">
	<strong>警告！</strong>
	无效的合同信息！
</div>
<#else>
<!-- 支出名目表单 -->
<form class="payout-names-dic-form hide" action="${request.contextPath}/config/dictionary/add" method="post">
	<input type="hidden" name="groupCode" value="${payoutNameDicKey}">
	<input type="hidden" name="system" value="false">
	<div class="row">
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">
							<span class="not-null">*</span>
							资金名目
						</div>
					</div>
					<input type="text" class="form-control" name="val" placeholder="最多30个汉字">
				</div>
			</div>
		</div>
	</div>
	<hr>
	<div class="row">
		<div class="col-md-12 text-right">
			<div class="btn btn-primary btn-sm btn-submit-payout-dic-form">确定</div>
			&nbsp;
			<div class="btn btn-default btn-sm btn-close-payout-dic-form">返回</div>
		</div>
	</div>
</form>

<form class="bid-payout-form" action="${request.contextPath}/contract/add-payout" method="post">
	<input type="hidden" name="contract.id" value="${contractId}" />
	<input type="hidden" name="payOut" value="true" />
	<input type="hidden" name="inAccount" value="true">
	<input type="hidden" name="invoice" value="true">
	<div class="row">
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">
							<span class="not-null">*</span>
							资金名目
						</div>
					</div>
					<select class="form-control" name="name">
						<#if payoutNames??> <#list payoutNames as pn>
						<option value="${pn}">${pn}</option>
						</#list> </#if>
					</select>
					<div class="input-group-addon btn btn-default new-payout-dic">
						<div class="text-danger">
							<i class="icon-plus"></i>
							新的名目
						</div>
					</div>
				</div>
			</div>
		</div>
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">
							<span class="not-null">*</span>
							支出金额
						</div>
					</div>
					<input type="text" class="form-control" name="total" placeholder="请输入支出金额">
				</div>
			</div>
		</div>
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">
							<span class="not-null">*</span>
							支出日期
						</div>
					</div>
					<input type="text" class="form-control datetimepicker" data-format="yyyy-MM-dd" data-foot="false" name="time" placeholder="请选择日期" readonly="readonly">
				</div>
			</div>
		</div>
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">收款单位/人</div>
					</div>
					<input type="text" class="form-control " name="source" placeholder="最多50个汉字">
				</div>
			</div>
		</div>
		<div class="col-md-12">
			<div class="form-group">
				<div class="input-group">
					<div class="input-group-addon">
						<div class="form-item-title">备注</div>
					</div>
					<textarea rows="5" class="form-control" name="remark" placeholder="最多500个汉字"></textarea>
				</div>
			</div>
		</div>
	</div>
	<hr>
	<div class="row">
		<div class="col-md-12 text-right">
			<div class="btn btn-primary btn-sm btn-submit">确定</div>
			&nbsp;
			<div class="btn btn-default btn-sm" onclick="$.suredy.closeModal();">关闭</div>
		</div>
	</div>
</form>
<script type="text/javascript">
	$(function() {
		// 初始化日期控件
		$.suredy.datetimepicker.init();

		// 切换支出字典表单
		$('.btn.new-payout-dic').on('click', function() {
			var dicForm = $('form.payout-names-dic-form');
			var payoutForm = $('form.bid-payout-form');

			payoutForm.fadeOut(200, function() {
				dicForm.get(0).reset();

				dicForm.removeClass('hide').fadeIn(200);
			});
		});

		// 切换支出表单
		$('.btn.btn-close-payout-dic-form').on('click', function() {
			var dicForm = $('form.payout-names-dic-form');
			var payoutForm = $('form.bid-payout-form');

			dicForm.fadeOut(200, function() {
				dicForm.get(0).reset();

				payoutForm.fadeIn(200);
			});
		});

		// 保存字典值
		$('.btn.btn-submit-payout-dic-form').on('click', function() {
			var $this = $(this);

			if ($this.hasClass('disabled'))
				return false;

			$this.addClass('disabled');

			var dicForm = $('form.payout-names-dic-form');
			var payoutForm = $('form.bid-payout-form');

			dicForm.ajaxSubmit({
				dataType : "json",
				success : function(data, textStatus, jqXHR) {
					if (!data || !data.success) {
						alert(data.msg || '添加新的资金名目失败！');
						$this.removeClass('disabled');
						return false;
					}

					var dic = data.data;

					// 保存到复选框中
					$('select[name="name"]').append('<option value="' + dic.val + '">' + dic.val + '</option>');
					$('select[name="name"] option').last().prop('selected', true);

					dicForm.fadeOut(200, function() {
						dicForm.get(0).reset();

						payoutForm.fadeIn(200);
					});

					$this.removeClass('disabled');
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert('添加新的资金名目失败！\n' + errorThrown);
					$this.removeClass('disabled');
					return false;
				}
			});
		});

		// 回收款项控制
		$('#isNeedGetBack').on('click', function() {
			var $this = $(this);
			var status = $('div.gave-back-status');
			var radio = $('input[name="gaveBack"]');

			if ($this.prop('checked')) {
				// 选中
				radio.eq(0).prop('checked', true);
				status.addClass('in');
			} else {
				// 未选中
				status.removeClass('in');
				radio.prop('checked', false);
			}
		});

		$('form.bid-payout-form .btn-submit').on('click', function() {
			var $this = $(this);

			if ($this.hasClass('disabled'))
				return false;

			$this.addClass('disabled');

			var form = $('form.bid-payout-form');

			form.ajaxSubmit({
				dataType : "json",
				success : function(data, textStatus, jqXHR) {
					if (!data || !data.success) {
						alert(data.msg || '记录费用信息失败!');
						$this.removeClass('disabled');
						return false;
					}

					$.suredy.refreshContent('${bidId}', [ '#tab3', 'div.bid-payout' ]);

					$.suredy.closeModal();
				},
				error : function(jqXHR, textStatus, errorThrown) {
					alert('记录费用信息失败!\n' + errorThrown);
					$this.removeClass('disabled');
					return false;
				}
			});
		});
	});
</script>
</#if>
